"""
 String数据类型 API测试
"""
import time

import redis

pool = redis.ConnectionPool(host="192.168.100.3", port=6379, decode_responses=True)
client = redis.Redis(connection_pool=pool)

# string类型操作：增，删，改，查
# 插入
client.set("key1","value1")

client.setnx("key2","value2" )
client.setnx("key1","value11" )
# 查询
# value = client.get("key1")
# print(value)
print(client.get("key1"))
print(client.get("key2"))

# 追加: value1-add
client.append("key1","-add")
print(client.get("key1"))
# 查询值的长度
print(client.strlen("key1"))
# 自增 incr incrby
client.set("score",90)
# 自增 +1
print(client.incr("score"))
print(client.incrby("score", 5))
print(client.incr("score1"))

# 自减：decr

# 批量插入:mset

client.mset(k1="v1",k2="v2")
# 批量查询
print(client.mget("k1"))
print(client.mget("k1","k2"))

client.mset(k3="abcdefg")
# 范围查找;
print(client.getrange("k3", 0, 2))
# 更新操作;
client.setrange("k3", 0 , "aaa")
print(client.getrange("k3",0 , -1))

# 设置key的同时，设置有效时间
client.setex("key4","value4",5)
print(client.ttl("key4"))
time.sleep(1)
print(client.ttl("key4"))
time.sleep(1)
print(client.ttl("key4"))


client.mset(k5="value5")
print(client.getset("k5","v5"))
print(client.get("k5"))
